<template>
  <view class="uni-container">
    <!-- 顶部标题 -->
    <view class="header">
      <text class="header-title">UniApp WebView 浏览器</text>
      <view class="platform-tag">H5 | 小程序 | App</view>
    </view>
    
    <!-- 地址栏 -->
    <view class="address-bar">
      <view class="nav-buttons">
        <button class="nav-btn" @click="goBack" :disabled="!canGoBack" title="后退">
          <uni-icons type="arrowleft" size="18" color="white"></uni-icons>
        </button>
        <button class="nav-btn" @click="goForward" :disabled="!canGoForward" title="前进">
          <uni-icons type="arrowright" size="18" color="white"></uni-icons>
        </button>
        <button class="nav-btn" @click="refresh" title="刷新">
          <uni-icons type="refresh" size="18" color="white"></uni-icons>
        </button>
      </view>
      
      <view class="url-container">
        <input 
          type="text" 
          class="url-input" 
          v-model="inputUrl" 
          placeholder="输入网址..."
          @confirm="loadUrl"
        />
        <button class="go-btn" @click="loadUrl">前往</button>
      </view>
    </view>
    
    <!-- WebView 区域 -->
    <view class="webview-container">
      <web-view 
        :src="currentUrl" 
        @load="onPageLoad" 
        @error="onPageError"
      ></web-view>
      
      <!-- 加载指示器 -->
      <view class="loading-overlay" v-if="loading">
        <view class="spinner"></view>
        <text class="loading-text">{{ loadingText }}</text>
      </view>
    </view>
    
    <!-- 状态栏 -->
    <view class="status-bar">
      <view class="status-text">
        <uni-icons type="lock-filled" size="14" color="#27ae60" v-if="isSecure"></uni-icons>
        <uni-icons type="unlocked" size="14" color="#e74c3c" v-else></uni-icons>
        <text>{{ isSecure ? '安全连接' : '非安全连接' }}</text>
      </view>
      <text>{{ pageTitle }}</text>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      inputUrl: 'https://m.baidu.com/',
      currentUrl: 'https://m.baidu.com/',
      loading: true,
      loadingText: '正在加载百度移动版...',
      pageTitle: '百度一下，你就知道',
      canGoBack: false,
      canGoForward: false,
      isSecure: true,
      history: []
    }
  },
  onLoad() {
    // 从本地存储加载历史记录
    try {
      const savedHistory = uni.getStorageSync('browserHistory');
      if (savedHistory) {
        this.history = JSON.parse(savedHistory);
      }
    } catch (e) {
      console.error('加载历史记录失败', e);
    }
  },
  methods: {
    loadUrl() {
      if (!this.inputUrl) return;
      
      let url = this.inputUrl.trim();
      
      // 确保URL格式正确
      if (!url.startsWith('http://') && !url.startsWith('https://')) {
        url = 'https://' + url;
      }
      
      // 更新当前URL
      this.currentUrl = url;
      this.inputUrl = url;
      this.loading = true;
      this.loadingText = `正在加载 ${url}...`;
      
      // 添加到历史记录
      this.addToHistory(url);
    },
    
    addToHistory(url) {
      // 避免重复添加
      if (this.history.includes(url)) return;
      
      // 限制历史记录数量
      this.history.unshift(url);
      if (this.history.length > 20) {
        this.history.pop();
      }
      
      // 保存到本地存储
      try {
        uni.setStorageSync('browserHistory', JSON.stringify(this.history));
      } catch (e) {
        console.error('保存历史记录失败', e);
      }
    },
    
    refresh() {
      this.loading = true;
      this.loadingText = '正在刷新页面...';
      
      // 重新设置URL来触发刷新
      const current = this.currentUrl;
      this.currentUrl = '';
      this.$nextTick(() => {
        this.currentUrl = current;
      });
    },
    
    goBack() {
      // 简化处理
      this.canGoBack = false;
    },
    
    goForward() {
      // 简化处理
      this.canGoForward = false;
    },
    
    onPageLoad(e) {
      this.loading = false;
      this.pageTitle = e.detail.title || '当前页面';
      this.isSecure = this.currentUrl.startsWith('https://');
    },
    
    onPageError(e) {
      this.loading = false;
      this.pageTitle = '加载失败';
      this.isSecure = false;
      uni.showToast({
        title: '加载页面失败',
        icon: 'none'
      });
    }
  }
}
</script>

<style>
/* 移除所有按钮的默认边框 */
button {
  border: none;
  background: none;
  padding: 0;
  margin: 0;
  line-height: 1;
}

/* 移除按钮点击后的蓝色边框 */
button::after {
  border: none;
}

.uni-container {
  width: 100%;
  height: 100vh;
  display: flex;
  flex-direction: column;
  background-color: white;
}

/* 顶部标题 */
.header {
  height: 60px;
  background: linear-gradient(90deg, #1a2a6c, #2a5298);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  border-bottom: 1px solid rgba(255,255,255,0.1);
}

.header-title {
  font-size: 22px;
  font-weight: 600;
  letter-spacing: 1px;
  text-shadow: 0 2px 4px rgba(0,0,0,0.2);
}

.platform-tag {
  position: absolute;
  right: 15px;
  background: rgba(255, 255, 255, 0.2);
  padding: 5px 15px;
  border-radius: 20px;
  font-size: 13px;
  font-weight: 500;
}

/* 地址栏 */
.address-bar {
  height: 60px;
  display: flex;
  align-items: center;
  padding: 0 15px;
  background: #2c3e50;
  border-bottom: 1px solid #34495e;
}

.nav-buttons {
  display: flex;
  gap: 8px;
  margin-right: 12px;
}

.nav-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: #3498db;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s ease;
}

.nav-btn:disabled {
  background: #7f8c8d;
  cursor: not-allowed;
}

.url-container {
  flex: 1;
  display: flex;
  height: 40px;
  border-radius: 20px;
  overflow: hidden;
  background: #ecf0f1;
  box-shadow: inset 0 2px 5px rgba(0, 0, 0, 0.1);
}

.url-input {
  flex: 1;
  padding: 0 15px;
  border: none;
  background: transparent;
  outline: none;
  font-size: 14px;
  color: #2c3e50;
}

/* 移除输入框的蓝色边框 */
.url-input:focus {
  outline: none;
  box-shadow: none;
}

.go-btn {
  width: 70px;
  background: #27ae60;
  color: white;
  font-weight: 600;
  cursor: pointer;
  border: none;
  outline: none;
}

/* WebView 区域 */
.webview-container {
  flex: 1;
  position: relative;
  background: #f8f9fa;
}

/* 加载指示器 */
.loading-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(255, 255, 255, 0.92);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 10;
}

.spinner {
  width: 50px;
  height: 50px;
  border: 5px solid rgba(52, 152, 219, 0.2);
  border-top: 5px solid #3498db;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 15px;
}

.loading-text {
  font-size: 16px;
  color: #2c3e50;
  font-weight: 500;
}

/* 状态栏 */
.status-bar {
  height: 30px;
  background: #34495e;
  color: #ecf0f1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 15px;
  font-size: 12px;
}

.status-text {
  display: flex;
  align-items: center;
  gap: 8px;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* 响应式调整 */
@media (max-width: 480px) {
  .nav-buttons {
    gap: 5px;
  }
  
  .nav-btn {
    width: 32px;
    height: 32px;
  }
  
  .url-container {
    height: 36px;
  }
  
  .go-btn {
    width: 60px;
    font-size: 13px;
  }
  
  .header-title {
    font-size: 18px;
  }
  
  .platform-tag {
    padding: 3px 10px;
    font-size: 11px;
  }
}
</style>